var usernameInput = $("[name='username']")
var passwordInput = $("[name='password']")
var repassInput = $("[name='repass']")
var username0
var password
var repass

// 用户名验证
$(usernameInput).focus(function () {
  $("[name='username']").next().text('')
})
$(usernameInput).blur(function () {
  username = $(usernameInput).val()
  if (username == '') {
    $("[name='username']").next().text('用户名不能为空')
    return false
  }
  var reg = /^1[3,5,7,8]\d{9}$/
  if (!reg.test(username)) {
    $("[name='username']").next().text('用户名格式错误')
    return false
  }
})
// 密码验证
$(passwordInput).focus(function () {
  $("[name='password']").next().text('')
})
$(passwordInput).blur(function () {
  password = $(passwordInput).val()
  if (password == '') {
    $("[name='password']").next().text('密码不能为空')
    return false
  }
  if (!/^[a-zA-Z]\w{5,15}/.test(password) || !/[0-9]/.test(password) || !/[a-zA-z]/.test(password)) {
    $("[name='password']").next().text('密码是由字母开头,数字字母组成的6-16位')
    return false
  }
})
// 确认密码
$(repassInput).blur(function () {
  repass = $(repassInput).val()
  if (repass != password) {
    $("[name='repass']").next().text('两次密码不一致')
    return false
  }
})
$(repassInput).focus(function () {
  $("[name='repass']").next().text('')
})




$('.zuce-btn').click(function () {


  if ($(usernameInput).val() == '') {
    layer.msg('用户名不能为空', {
      time: 1000
    })
    return false
  }
  if ($(passwordInput).val() == '') {
    layer.msg('密码不能为空', {
      time: 1000
    })
    return false
  }
  if ($(repassInput).val() != $(passwordInput).val()) {
    layer.msg('两次密码不一致', {
      time: 1000
    })
    return false
  }

  if (!$('.agree').prop('checked')) {
    layer.msg('请阅读小米协议并同意', {
      icon: 5,
      time: 1000
    });
    return false
  }

  var loadindex = layer.load(1, {
    shade: [1, 'rgb(161, 157, 157)']
  })
  $.ajax({
    url: './php/zuce.php',
    method: "get",
    data: {
      username,
      password
    },
    success: function (res) {
      var { meta: { status, msg }, data } = res
      layer.close(loadindex)
      if (status == 2) {
        layer.msg('注册成功', {
          icon: 1,
          time: 1500
        }, function () {
          $('.agree').prop('checked',false)
          $("[name='username']").val('')
          $("[name='password']").val('')
          $("[name='repass']").val('')
          location.href = 'denglu.html'
        })
      } else if (status == 1) {
        layer.alert('用户名已存在', {
          icon: 0,
          time: 1500
        })
      }
    },
    error(xhr, err, msg) {
      console.log('注册失败');
    },
    dataType: 'json',
  })


  return false
})
